<?php
// ------------------------------------------------------------------------
// |@Author       : Jarmin <edshop@qq.com>
// |@----------------------------------------------------------------------
// |@Date         : 2025-05-04 22:47:12
// |@----------------------------------------------------------------------
// |@LastEditTime : 2025-05-04 23:21:54
// |@----------------------------------------------------------------------
// |@LastEditors  : Jarmin <jarmin@ladmin.cn>
// |@----------------------------------------------------------------------
// |@Description  : 
// |@----------------------------------------------------------------------
// |@FilePath     : AuthController.php
// |@----------------------------------------------------------------------
// |@Copyright (c) 2025 http://www.ladmin.cn   All rights reserved. 
// ------------------------------------------------------------------------
declare(strict_types=1);

namespace think\admin\test\controller;

use think\admin\Controller;
use think\admin\test\service\UserService;
use think\Exception;
use think\facade\Cache;
use think\Response;

class AuthController extends Controller
{
    /**
     * 初始化
     */
    protected function initialize(): void
    {
        $this->service = new UserService();
    }

    /**
     * 登录
     * @return Response
     */
    public function login(): Response
    {
        try {
            // 获取请求数据
            $data = $this->request->post();

            // 验证数据
            $this->validate($data, 'User.login');

            // 验证验证码
            if (!$this->checkCaptcha($data['captcha'])) {
                return $this->error('验证码错误');
            }

            /** @var UserService $service */
            $service = $this->service;
            $result = $service->login($data);

            return $this->success($result, '登录成功');
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 注册
     * @return Response
     */
    public function register(): Response
    {
        try {
            // 获取请求数据
            $data = $this->request->post();

            // 验证数据
            $this->validate($data, 'User.register');

            // 验证验证码
            if (!$this->checkCaptcha($data['captcha'])) {
                return $this->error('验证码错误');
            }

            /** @var UserService $service */
            $service = $this->service;
            $result = $service->register($data);

            return $this->success($result, '注册成功');
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 验证码校验
     * @param string $code
     * @return bool
     */
    protected function checkCaptcha(string $code): bool
    {
        $key = $this->request->header('captcha-key');
        if (!$key) {
            return false;
        }

        $captcha = Cache::get('captcha_' . $key);
        if (!$captcha) {
            return false;
        }

        Cache::delete('captcha_' . $key);
        return strtolower($code) === strtolower($captcha);
    }
} 